package com.examiner.service.impl;

import com.examiner.bean.CommonPerson;
import com.examiner.bean.Department;
import com.examiner.bean.Job;
import com.examiner.bean.test.*;
import com.examiner.bean.vo.EditNewTestVo;
import com.examiner.bean.vo.JobVO;
import com.examiner.bean.vo.NewTestVo;
import com.examiner.dao.*;
import com.examiner.page.QueryInfo;
import com.examiner.service.NewTestService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class NewTestServiceImpl implements NewTestService {

    @Resource
    private NewTestDao newTestDao;

    @Resource
    private NewTestUserDao newTestUserDao;

    @Resource
    private NewTestDepartmentDao newTestDepartmentDao;

    @Resource
    private NewTestItemDao newTestItemDao;

    @Resource
    private NewTestJobDao newTestJobDao;

    @Resource
    private TestTypeDao testTypeDao;

    @Resource
    private TestItemDao testItemDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNewTest(NewTestVo newTestVo) {
        NewTest newTest = new NewTest();
        NewTestItem newTestItem;
        NewTestDepartment newTestDepartment;
        NewTestJob newTestJob;
        // 保存新建考评
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            newTest.setCreateTime(formatter.format(date));
            newTest.setStartTime(newTestVo.getStartTime());
            newTest.setEndTime(newTestVo.getEndTime());
            newTest.setExaminerEndTime(newTestVo.getExaminerEndTime());
            newTest.setTestId(newTestVo.getTestId());
            newTest.setTestTitle(newTestVo.getTestTitle());
            newTest.setTestCreate(newTestVo.getTestCreate());
            newTest.setTestTotalGrade(newTestVo.getTestTotalGrade());
            newTest.setNewTestCreate(newTestVo.getNewTestCreate());
            NewTest save = newTestDao.save(newTest);
            NewTestUser user;
            // 生成每个考评对象的考评记录
            for (CommonPerson item : newTestVo.getUsers()) {
                user = new NewTestUser();
                user.setUid(item.getId());
                user.setEid(item.getEid());
                user.setExaminer(item.getExaminer());
                user.setUserName(item.getUserName());
                user.setType("成员");
                user.setNid(save.getId());
                user.setTid(save.getTestId());
                user.setTestTotalGrade(newTest.getTestTotalGrade());
                user.setMyTotalGrade(0);
                user.setExaminerTotalGrade(0);
                user.setCommitTime("");
                user.setExaminerCommitTime("");
                user.setState(0);
                user.setSid(item.getSId());
                user.setStype(item.getStype());
                NewTestUser ntu = newTestUserDao.save(user);
                // TODO 每个用户 根据testId 创建 testItem
                List<TestType> testTypes = testTypeDao.findBySid(newTest.getTestId());
                for (TestType testType : testTypes) {
                    List<TestItem> testItems = testItemDao.findBySid(testType.getTestTypeId());
                    for (TestItem testItem : testItems) {
                        newTestItem = new NewTestItem();
                        newTestItem.setNtuid(ntu.getId());
                        newTestItem.setTestItemName(testItem.getTestItemName());
                        newTestItem.setTestItemGrade(testItem.getTestItemGrade());
                        newTestItem.setSid(testItem.getSid());
                        newTestItem.setMyGrade(0);
                        newTestItem.setExaminerGrade(0);
                        newTestItemDao.save(newTestItem);
                    }
                }
            }
            // 生成选择的部门记录
            if (newTestVo.getDepartments().size() > 0){
                for (Department department : newTestVo.getDepartments()) {
                    newTestDepartment = new NewTestDepartment();
                    newTestDepartment.setDid(department.getId());
                    newTestDepartment.setUserName(department.getUserName());
                    newTestDepartment.setEid(department.getEid());
                    newTestDepartment.setExaminer(department.getExaminer());
                    newTestDepartment.setNid(newTest.getId());
                    newTestDepartment.setTid(newTestVo.getTestId());
                    newTestDepartment.setType("部门");
                    newTestDepartmentDao.save(newTestDepartment);
                }
            }
            // 生成当前考评的职位记录
           if (newTestVo.getJobs().size() > 0){
               for (JobVO job : newTestVo.getJobs()) {
                   newTestJob = new NewTestJob();
                   newTestJob.setEid(job.getEid());
                   newTestJob.setExaminer(job.getExaminer());
                   newTestJob.setJid(job.getId());
                   newTestJob.setNid(newTest.getId());
                   newTestJob.setTid(newTestVo.getTestId());
                   newTestJob.setType(job.getType());
                   newTestJob.setUserName(job.getUserName());
                   newTestJobDao.save(newTestJob);
               }
           }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    /**
     * 获取新建考评列表
     * @param userName
     * @param query
     * @return
     * @throws Exception
     */
    @Override
    public List<NewTest> getNewTestList(String userName, QueryInfo query) throws Exception {
        return newTestDao.getNewTestList(userName,(query.getPageNum()-1)* query.getPageSize(),query.getPageSize(),query.getQuery());
    }

    @Override
    public int getNewTestListTotal(String userName, QueryInfo query) throws Exception {
        return newTestDao.getNewTestListTotal(userName,query.getQuery());
    }

    @Override
    public NewTest getNewTestById(Integer id) {
        return newTestDao.getOne(id);
    }

    @Override
    public NewTestUND getNewTestVoById(Integer id) {
        NewTestUND newTestUND = new NewTestUND();
        List<NewTestUser> users = newTestUserDao.findAllByNid(id);
        List<NewTestDepartment> departments = newTestDepartmentDao.findAllByNid(id);
        List<NewTestJob> jobs =  newTestJobDao.findAllByNid(id);
        newTestUND.setUsers(users);
        newTestUND.setDepartments(departments);
        newTestUND.setJobs(jobs);
        return newTestUND;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeNewTestInfo(EditNewTestVo editInfo) throws Exception{
        NewTest save = null;
        NewTestUser newTestUser;
        NewTestJob newTestJob;
        NewTestDepartment newTestDepartment;
        NewTestItem newTestItem;
        List<TestItem> testItems = new ArrayList<>();
        NewTest one = newTestDao.getOne(editInfo.getNewTest().getId());
        // 当考评库内容不一样时
        // 修改考评内容
        if (!one.getTestId().equals(editInfo.getNewTest().getTestId())){
            one.setTestId(editInfo.getNewTest().getTestId());
            one.setTestCreate(editInfo.getNewTest().getTestCreate());
            one.setTestTitle(editInfo.getNewTest().getTestTitle());
            one.setTestTotalGrade(editInfo.getNewTest().getTestTotalGrade());
            save = newTestDao.save(one);
            // 修改每个用户的考评项
        }
        if (save != null){
            List<TestType> testTypes = testTypeDao.findBySid(editInfo.getNewTest().getTestId());
            if (testTypes.size() != 0 ){
                      for (TestType testType : testTypes) {
                    List<TestItem> bySid = testItemDao.findBySid(testType.getTestTypeId());
                    testItems.addAll(bySid);
                }
            }
        }
        // 修改 考评对象的考评员
        if (editInfo.getUsers().size() != 0){
            for (NewTestUser user : editInfo.getUsers()) {
                newTestUser = newTestUserDao.getOne(user.getId());
                if (!newTestUser.getEid().equals(user.getEid())){
                    newTestUser.setEid(user.getEid());
                    newTestUser.setExaminer(user.getExaminer());
                }
                if (save != null){
                    newTestUser.setTid(one.getTestId());
                    newTestUser.setTestTotalGrade(save.getTestTotalGrade());
                    newTestItemDao.deleteByNtuid(user.getId());
                    if (testItems.size() != 0 ){
                        for (TestItem testItem : testItems) {
                            newTestItem = new NewTestItem();
                            newTestItem.setNtuid(user.getId());
                            newTestItem.setTestItemName(testItem.getTestItemName());
                            newTestItem.setTestItemGrade(testItem.getTestItemGrade());
                            newTestItem.setSid(testItem.getSid());
                            newTestItem.setMyGrade(0);
                            newTestItem.setExaminerGrade(0);
                            newTestItemDao.save(newTestItem);
                        }
                    }
                }
                newTestUserDao.save(newTestUser);
            }
        }
        // 修改 部门的考评对象记录
        if (editInfo.getDepartments().size() != 0){
            for (NewTestDepartment department : editInfo.getDepartments()) {
                newTestDepartment = newTestDepartmentDao.getOne(department.getId());
                if (!newTestDepartment.getEid().equals(department.getEid())){
                    newTestDepartment.setEid(department.getEid());
                    newTestDepartment.setExaminer(department.getExaminer());
                    newTestDepartmentDao.save(newTestDepartment);
                }
            }
        }
        // 修改 部门的考评职位记录
        if (editInfo.getJobs().size() != 0){
            for (NewTestJob job : editInfo.getJobs()) {
                newTestJob = newTestJobDao.getOne(job.getId());
                if (!newTestJob.getEid().equals(job.getEid())){
                    newTestJob.setEid(job.getEid());
                    newTestJob.setExaminer(job.getExaminer());
                    newTestJobDao.save(newTestJob);
                }
            }
        }
    }

    /**
     * 删除
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteNewTestById(Integer id) throws Exception {
        // 删除新建考评记录
        newTestDao.deleteById(id);
        // 删除新建考评的考评对象记录
        List<NewTestUser> users = newTestUserDao.findAllByNid(id);
        if (users.size() != 0){
            for (NewTestUser user : users) {
                newTestItemDao.deleteByNtuid(user.getId());
            }
        }
        newTestUserDao.deleteAllByNid(id);
        newTestDepartmentDao.deleteAllByNid(id);
        newTestJobDao.deleteAllByNid(id);
    }
}
